package com.xgst.land.dialog

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.xgst.land.R
import com.xgst.land.bean.IceboxItemListBean

class ItemListDialog(context:Context):BaseDialog(context), OnItemClickListener {
    override fun getLayoutRes() = R.layout.dialog_item_list_layout
    private var adapter:ItemAdapter
    private var onClick:(String,Int)->Unit = {name,id -> }
    init {
        val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)
        adapter = ItemAdapter()
        adapter.setOnItemClickListener(this)
        recyclerView.adapter = adapter
    }
    fun show(data:MutableList<IceboxItemListBean>, onClick:(String, Int)->Unit) {
        super.show()
        this.onClick = onClick
        adapter.setNewInstance(data)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as IceboxItemListBean
        if(item.disable.isEmpty()){
            onClick(item.name,item.id)
            dismiss()
        }
    }
}
class ItemAdapter : BaseQuickAdapter<IceboxItemListBean,BaseViewHolder>(R.layout.item_list_layout){
    override fun convert(holder: BaseViewHolder, item: IceboxItemListBean) {
        val line = holder.getView<View>(R.id.line)
        if(getItemPosition(item) == 0){
           line.visibility = View.GONE
        }else{
            line.visibility = View.VISIBLE
        }
        holder.setText(R.id.tvName,item.name)
        if(TextUtils.isEmpty(item.disable)){
            holder.setTextColor(R.id.tvName,Color.BLACK)
        }else{
            holder.setTextColor(R.id.tvName,context.getColor(R.color.colorTextGray))
        }
    }
}